﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Foundation.Entity;
using FishWebLib;
using System.Data;
using System.Collections.Specialized;

namespace SGRapidForm.Foundation.Component
{
    public class FoundationClassComponet : ComponentBase
    {
        /// <summary>
        /// 查询所有基础资料类别
        /// </summary>
        /// <returns>基础资料类别集合</returns>
        public List<FoundationClassEntity> GetFoundationClassList()
        {
            return FishBLLHelper.CallSpGetDataItemList<FoundationClassEntity>("sgrf_foundation_FoundationClass_GetAll", null);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询基础资料类别
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>类别集合</returns>
        public List<FoundationClassEntity> PagingGetFoundationClass(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = "[Order] ASC";
            }
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassCode", DbType.String, " AND FoundationClassCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassName", DbType.String, " AND FoundationClassName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Order", DbType.Int32, " AND Order = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Remark", DbType.String, " AND Remark like '%{0}%' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<FoundationClassEntity>("sgrf_foundation_FoundationClass_PagingGetFoundationClass", null, startRecord, maxRecords, sortColumns, filterConditions);
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "foundationclasscode":
                    case "foundationclassname":
                    case "remark":
                    case "isys":
                    case "order":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤，排序、分页查询基础资料类别记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int GetFoundationClassCount(String filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassCode", DbType.String, " AND FoundationClassCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "FoundationClassName", DbType.String, " AND FoundationClassName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Order", DbType.Int32, " AND Order = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "IsSys", DbType.Boolean, " AND IsSys = '{0}' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Remark", DbType.String, " AND Remark like '%{0}%' "));
            filterConditions = sql.ToString();

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_foundation_FoundationClass_PagingGetFoundationClassCount", null, filterConditions));
        }

        /// <summary>
        /// 根据Keyid获取一位用户
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>一位用户</returns>
        public FoundationClassEntity GetFoundationClassByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<FoundationClassEntity>("sgrf_foundation_FoundationClass_GetFoundationClassByKeyid", null, keyid);
        }

        /// <summary>
        /// 删除一位用户
        /// </summary>
        /// <param name="keyid">用户的Keyid</param>
        /// <returns>是否删除成功</returns>
        public bool DeleteFoundationClass(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_FoundationClass_Delete", null, keyid) > 0);
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool BulkDeleteFoundation(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_FoundationClass_BulkDelete", null, keyids) == count);
        }

        /// <summary>
        /// 更新一位用户
        /// </summary>
        /// <param name="UserDetails">一位用户</param>
        /// <returns>是否更新成功</returns>
        public bool UpdateFoundationClass(FoundationClassEntity foundationClass)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_FoundationClass_Update", foundationClass) > 0);
        }

        /// <summary>
        /// 新增一条基础资料类别数据
        /// </summary>
        /// <param name="foundationClass">一条类别数据的记录信息</param>
        /// <returns>是否新增成功</returns>
        public bool AddFoundationClass(FoundationClassEntity foundationClass)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_foundation_FoundationClass_Add", foundationClass) > 0);
        }
    }
}
